// 修改Order.js文件
const { Model, DataTypes } = require('sequelize');

// 导出初始化函数
module.exports = (sequelize, DataType) => {
    class Order extends Model {
        // 添加生成订单号的静态方法
        static generateOrderNumber() {
            const timestamp = Date.now();
            const random = Math.floor(Math.random() * 1000);
            return `ORD-${timestamp}-${random}`;
        }
    }

    // 初始化模型
    Order.init({
        id: {
            type: DataType.INTEGER,
            primaryKey: true,
            autoIncrement: true
        },
        orderNumber: {
            type: DataType.STRING(50),
            allowNull: false,
            unique: true,
            defaultValue: () => Order.generateOrderNumber()
        },
        userId: {
            type: DataType.INTEGER,
            allowNull: false,
            references: {
                model: 'users',
                key: 'id'
            }
        },
        status: {
            type: DataTypes.ENUM(
                'pending',      // 待付款
                'paid',         // 已付款
                'processing',   // 处理中
                'shipped',      // 已发货
                'delivered',    // 已送达
                'cancelled',    // 已取消
                'refunded'      // 已退款
            ),
            defaultValue: 'pending'
        },
        totalAmount: {
            type: DataTypes.DECIMAL(10, 2),
            allowNull: false,
            validate: {
                // 修改验证规则，使用自定义验证函数替代min验证器
                isNonNegative(value) {
                    if (parseFloat(value) < 0) {
                        throw new Error('订单总额不能为负数');
                    }
                }
            }
        },
        subtotal: {
            type: DataTypes.DECIMAL(10, 2),
            allowNull: false
        },
        taxAmount: {
            type: DataTypes.DECIMAL(10, 2),
            allowNull: false
        },
        shippingAmount: {
            type: DataTypes.DECIMAL(10, 2),
            allowNull: false
        },
        paymentMethod: {
            type: DataTypes.STRING(50),
            allowNull: false
        },
        paymentStatus: {
            type: DataTypes.STRING(50),
            defaultValue: 'pending'
        },
        shippingAddress: {
            type: DataTypes.JSON,
            allowNull: false
        },
        notes: {
            type: DataTypes.TEXT,
            allowNull: true
        },
        paidAt: {
            type: DataTypes.DATE,
            allowNull: true
        },
        shippedAt: {
            type: DataTypes.DATE,
            allowNull: true
        },
        deliveredAt: {
            type: DataTypes.DATE,
            allowNull: true
        }
    }, {
        sequelize,
        modelName: 'Order',
        tableName: 'orders',
        timestamps: true,
        indexes: [
            { fields: ['userId'] },
            { fields: ['orderNumber'] },
            { fields: ['status'] }
        ],
        hooks: {
            beforeCreate: (order) => {
                if (!order.orderNumber) {
                    order.orderNumber = Order.generateOrderNumber();
                }
            }
        }
    });

    return Order;
};